package com.briup.cms.web.controller;

import com.briup.cms.bean.Category;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.CategoryService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import com.briup.cms.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "Category")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService service;

    //注册一个类别
    @ApiOperation(value = "注册类别",notes = "传递一个json对象的字符串")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/saveOrUpdateCategory")
    public Result saveOrUpdateCategory(@RequestBody Category category, HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.saveOrUpdateCategory(category);
        return Result.success(category);
    }

    //根据类别name查找具体类别信息
    @ApiOperation(value = "查找类别",notes = "传递一个字符串的name")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "类别name",name = "name")
    )
    @GetMapping("/findCategoryByUsername")
    public Result findCategoryByUsername(String name,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        Category category = service.findCategoryByUsername(name);
        if (category==null){
            throw new ServiceException(ResultCode.CATEGORY_NOT_EXIST);
        }
        return Result.success(category);
    }

    //分页查找所有类别
    @ApiOperation(value = "分页查找所有类别",notes = "传入第几页")
    @GetMapping("/findCategoryAll")
    public Result findCategoryAll(Integer pageNum){
        Page<Category> page = service.findAll(pageNum,2);
        return Result.success(page);
    }

    @ApiOperation(value = "通过Id删除对象",notes = "传入要删除对象的Id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @DeleteMapping("/deleteCategoryById/{id}")
    public Result deleteCategoryById(@PathVariable Long id,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteCategoryById(id);
        return Result.success("单个删除成功");
    }

    @ApiOperation(value = "通过Id批量删除对象",notes = "传入要删除对象的Id的集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/deleteCategoryInBatch")
    public Result deleteCategoryInBatch(@RequestBody List<Long> ids,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteCategoryInBatch(ids);
        return Result.success("批量删除成功");
    }

    @ApiOperation(value = "修改分类no",notes = "传入id和no")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/updateCategoryStatus")
    public Result updateCategoryStatus(Long id,int no,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.updateCategoryNo(id,no);
        return Result.success("update成功");
    }



    @ApiOperation(value = "按序号升序查询分类信息,并分页",notes = "传入页码和每页记录数")
    @GetMapping("/findAllSortbyno")
    public Result findAllSortbyno(int pageNum){
        Page<Category> page = service.findAllSortbyno(pageNum,2);
        return Result.success(page);
    }
}
